package com.tms.app.tms.ui;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.KeyEvent;
import android.widget.Toast;

import com.tms.app.tms.ui.home.MainActivity;
import com.tms.app.tms.utils.ToolsUtil;

/**
 * Created by Administrator on 2018/3/15 0015.
 */

public class InstallAc extends Activity {



    String apkPath;
    private ProgressDialog pd3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apkPath=getIntent().getStringExtra("apkFile");
        if (apkPath!=null){
            pd3 = ProgressDialog.show(this, "提示", "即将安装请稍候", false, true);
        }else {
            Toast.makeText(this,"更新安装失败，请重试",Toast.LENGTH_SHORT).show();
        }
        checkIsAndroidO();

    }


    /**
     * 判断是否是8.0,8.0需要处理未知应用来源权限问题,否则直接安装
     */
    private void checkIsAndroidO() {
        if (Build.VERSION.SDK_INT >= 26) {
            boolean b = getPackageManager().canRequestPackageInstalls();
            if (b) {
                ToolsUtil.installApk(this, apkPath);
            } else {
                //请求安装未知应用来源的权限
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 10010);
            }
        } else {
            ToolsUtil.installApk(this, apkPath);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 10010:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    ToolsUtil.installApk(this, apkPath);
                } else {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                    startActivityForResult(intent, 10012);
                }
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10012:
                checkIsAndroidO();
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN){
          if (pd3!=null&&pd3.isShowing()){
              pd3.dismiss();
              pd3=null;
          }
        }
        return super.onKeyDown(keyCode, event);
    }
}
